package com.ccepc.algmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ccepc.algmanage.service.AlgConfigPointsService;
import com.ccepc.common.core.util.R;
import com.ccepc.common.log.annotation.SysLog;
import com.ccepc.demo.api.entity.AlgConfigPoints;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 截图区域配置
 */
@RestController
@RequestMapping("/configPoints" )
@Api(value = "algorithmconfig", tags = "截图区域配置")
public class AlgConfigPointsController {
    @Resource
    private AlgConfigPointsService algConfigPointsService;

    @ApiOperation(value = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfigpoint_edit')" )
    public R save(@RequestBody AlgConfigPoints algConfigPoints) {
        return R.ok(algConfigPointsService.update(
                new LambdaUpdateWrapper<AlgConfigPoints>()
                        .eq(AlgConfigPoints::getDeviceNo, algConfigPoints.getDeviceNo())
                        .set(algConfigPoints.getPoints()!=null, AlgConfigPoints::getPoints, algConfigPoints.getPoints()))
        );
    }

    @ApiOperation(value = "通过摄像机编码查询图像区域")
    @GetMapping("/{deviceNo}" )
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfigpoint_view')" )
    public R getById(@PathVariable("deviceNo" ) String deviceNo) {
        AlgConfigPoints algConfigPoints = algConfigPointsService.getOne(
                new LambdaQueryWrapper<AlgConfigPoints>()
                        .eq(AlgConfigPoints::getDeviceNo, deviceNo));
        if (algConfigPoints == null || algConfigPoints.getPoints() == null) {
            algConfigPoints = new AlgConfigPoints();
            algConfigPoints.setPoints("");
            algConfigPoints.setDeviceNo(deviceNo);
        }
        return R.ok(algConfigPoints);
    }



}
